<?php

/*
 CI Library Image functions to help with images
 Written by: Ciprian Dimofte - 01-20-2009
 */
class Image {
	
	public $CI;
	public $image_file;
	public $sz;
	public $quality = 75;
	
	public function __construct(){
		
		$this->CI =& get_instance();
		
	}
	
	public function size($image_file, $increase_w_factor = 0, $increase_h_factor = 0){
		$this->image_file = $image_file;
		$imgDat = @getimagesize($this->image_file);
		
		return array("width"=>$imgDat[0]+$increase_w_factor, "height"=>$imgDat[1]+$increase_h_factor);
		
	}
	
	public function thumb($image_file , $sz ="-1", $quality = "75"){
		
		$this->image_file = $image_file;
		$this->sz = $sz;
		$this->quality = $quality;
		
		
		$imgDat = @getimagesize($this->image_file);
		$mime = $imgDat['mime'];
		   
		if ($imgDat[0] > $imgDat[1]+20) {
		  $tw = $this->sz;
		  $th = $imgDat[1]/($imgDat[0]/$tw);
		} else {
		  $th = $this->sz;
		  $tw = $imgDat[0]/($imgDat[1]/$th);
		}
	
		if ($this->sz == "-1") {
			$imNew = ImageCreateTrueColor($imgDat[0], $imgDat[1]);
		}else{
			$imNew = ImageCreateTrueColor($tw, $th);
		}
		   
		if ($mime == 'image/gif') {
		  $imFile= ImageCreateFromGIF($this->image_file);
		} else if ($mime == 'image/png') {
		  $imFile= ImageCreateFromPNG($this->image_file);
		} else if ($mime == 'image/jpeg') {
		  $imFile= ImageCreateFromJpeg($this->image_file);
		}
		  
		if ($this->sz == "-1") {
		  $res = imagecopyresampled ($imNew, $imFile, 0, 0, 0, 0, $imgDat[0], $imgDat[1], $imgDat[0], $imgDat[1]);
		} else {
		  $res = imagecopyresampled ($imNew, $imFile, 0, 0, 0, 0, $tw, $th, $imgDat[0], $imgDat[1]);
		}
		
		
		
		
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		header("Cache-Control: no-store, no-cache, must-revalidate"); 
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		header("Content-Type: ".$mime);
		header('Content-Disposition: inline; filename=test');
		
		
        
	    if ($mime == 'image/gif') {
	      imagealphablending($imNew, false);
          imagesavealpha($imNew,true);
          $transparent = imagecolorallocate($imNew, 0, 0, 0);
          imagecolortransparent($imNew, $transparent);
          
          $imFile= ImageCreateFromGIF($this->image_file);
          imagegif($imNew);
        } else if ($mime == 'image/png') {
          imagealphablending($imNew, false);
          imagesavealpha($imNew,true);
          $transparent = imagecolorallocate($imNew, 0, 0, 0);
          imagecolortransparent($imNew, $transparent);
          
          $imFile= ImageCreateFromPNG($this->image_file);
          imagepng($imNew);
        } else if ($mime == 'image/jpeg') {
          $imFile= ImageCreateFromJpeg($this->image_file , NULL, $this->quality);
          imagejpeg($imNew);
        }
		
		
		imagedestroy($imNew);
	}

}

?>